package rabbitMQ.entity;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.TimeoutException;

public class Main {

    public static void main1(String[] args) throws IOException, TimeoutException {

        String body = HttpRequest.get("https://apps.ep.saikul.com/api/sinzetech-order/jlorder/purchase?current=1&size=100&batchType=1&timeType=0&isMe=false&orderFinanceStatus=2&keyword=")
                .header("sinzetech-auth", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOiI3MjIyNzY0NjQwMjciLCJ0ZW5hbnRfbmFtZSI6Iuato-WNjuS-m-W6lOmTvueuoeeQhijpnZLlspsp5pyJ6ZmQ5YWs5Y-4Iiwib3Blbl9pZCI6bnVsbCwidXNlcl9uYW1lIjoiMTgyMjIyNzE3MTYiLCJyZWFsX25hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJhdXRob3JpdGllcyI6WyLnrqHnkIblkZjop5LoibIiXSwiY2xpZW50X2lkIjoic2luemV0ZWNoLWVycCIsInJvbGVfbmFtZSI6IueuoeeQhuWRmOinkuiJsiIsImxpY2Vuc2UiOiJwb3dlcmVkIGJ5IHNpbnpldGVjaCIsInVzZXJfaWQiOiIxNDk4NDkzODI0ODA5NDYzODEwIiwicm9sZV9pZCI6IjE1MjM5NTEyODQ4MjczMzY3MDYsMTUyMzk1MTI4NDgyNzMzNjcwNiwxNTIzOTUxMjg0ODI3MzM2NzA2LDE1MjM5NTEyODQ4MjczMzY3MDYsMTUyMzk1MTI4NDgyNzMzNjcwNiwxNTIzOTUxMjg0ODI3MzM2NzA2LDE1MjM5NTEyODQ4MjczMzY3MDYsMTUyMzk1MTI4NDgyNzMzNjcwNiwxNTIzOTUxMjg0ODI3MzM2NzA2LDE1MjM5NTEyODQ4MjczMzY3MDYsMTUyMzk1MTI4NDgyNzMzNjcwNiwxNTIzOTUxMjg0ODI3MzM2NzA2Iiwic2NvcGUiOlsiYWxsIl0sIm5pY2tfbmFtZSI6Iui2hee6p-euoeeQhuWRmCIsInVuaW9uX2lkIjpudWxsLCJvYXV0aF9pZCI6IiIsImV4cCI6MTY4MTI2NjU5MiwianRpIjoiN2Y5ZmJmNWYtZjEyMC00NDg2LTk2OWQtMDQwZDViZjdkMTg5IiwiYWNjb3VudCI6IjE4MjIyMjcxNzE2In0.OrnbX_PChJo0MHZTsbNWY2HWx2m6wBjZFWSK0y-X-8o")
                .execute().body();
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(body);
        cn.hutool.json.JSONObject jsonObject1 = JSONUtil.parseObj(jsonObject.get("data").toString());
        String records = jsonObject1.get("records").toString();
        JSONArray objects = JSONUtil.parseArray(records);
        for (Object object : objects) {
            String xzswOrderId = JSONUtil.parseObj(object.toString()).get("xzswOrderId").toString();
            sendMqMessage(Integer.valueOf(xzswOrderId));
        }

//

//        for (Integer orderId : orderIds) {
//            sendMqMessage(orderId);
//        }
    }

    public static void sendMqMessage(Integer orderId) throws IOException, TimeoutException {
        OrderContractSignedEvent orderContractSignedEvent = new OrderContractSignedEvent();
        orderContractSignedEvent.setOrderId(orderId);
        String event = JSONObject.toJSONString(orderContractSignedEvent);
        // 发送MQ

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("121.40.248.250");
        connectionFactory.setPort(5672);
        // todo 674.15
        connectionFactory.setUsername("");
        connectionFactory.setPassword("");
        Connection connection = connectionFactory.newConnection();
        System.out.println(connection);
        Channel channel = connection.createChannel();
//        channel.("order-contract-signed.sinzetech-prd", true, false, false, null);
        channel.basicPublish("","order-contract-signed.sinzetech-prd",null,event.getBytes());
        channel.close();
        connection.close();


    }

    public static void main(String[] args) {
        HashMap<Integer, Integer> objectObjectHashMap = new LinkedHashMap<>();

        for (int i = 0; i < 5; i++) {
            objectObjectHashMap.put(i, i);
        }
        objectObjectHashMap.forEach((k,v)->{
            System.out.println(k + ":" + v);
        });

    }


}
